package com.yoso.file.sdk.bo;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.compress.utils.Lists;

import java.util.List;

/**
 * <p>
 * 导入数据预览
 * </p>
 *
 * @author shizejin
 * @version 1.0
 * @date 2023/5/24 10:13
 */
@Data
@Accessors(chain = true)
public class ExportData {

    /**
     * 操作人
     */
    private String operator;

    /**
     * 操作时间
     */
    private String operateTime;

    /**
     * 数据环境来源
     */
    private String source;

    /**
     * 业务编码
     */
    private String businessCode;

    /**
     * 数据
     */
    private Object data;


    public static ExportData init(Object data, String businessCode, String operator) {
        return new ExportData().setData(data).setOperator(operator)
                .setBusinessCode(businessCode).setOperateTime(DateUtil.now());
    }

    public String parseContent() {
        String fmt = "【%s】-【%s】-【%s】";
        return String.format(fmt, source, operator, operateTime);
    }

    public <V> List<V> parseList(Class<V> cls) {
        if (data != null) {
            return JSON.parseArray(JSON.toJSONString(data), cls);
        }
        return Lists.newArrayList();
    }

    public <V> V parseObj(Class<V> cls) {
        if (data != null) {
            return JSON.parseObject(JSON.toJSONString(data), cls);
        }
        return null;
    }
}